package com.yuexun.dhdoctor.module.personalPlan.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.adapter.MyShareDeleteAdapter;
import com.yuexun.dhdoctor.module.personalPlan.bean.MyShareBean;
import com.yuexun.dhdoctor.module.personalPlan.bean.ShareBean;
import com.yuexun.dhdoctor.widget.LoadingDialog;
import com.yuexun.dhdoctor.widget.PullToRefresh.PullToRefreshList;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/9/13.
 */
public class Act_MyShare_DeleteMember extends BaseKJActivity {
    @Bind(R.id.myshare_delete_list)
    PullToRefreshList myshareDeleteList;

    private ListView mList;

    ArrayList<MyShareBean.DatasEntity> datasEntities = new ArrayList<MyShareBean.DatasEntity>();
    ArrayList<MyShareBean.DatasEntity> datasEntities_change = new ArrayList<MyShareBean.DatasEntity>();
    MyShareDeleteAdapter adapter;

    LoadingDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_myshare_deletemember);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        dialog = new LoadingDialog(aty);
        datasEntities = (ArrayList<MyShareBean.DatasEntity>) getIntent().getExtras().getSerializable("persons");
        datasEntities_change = (ArrayList<MyShareBean.DatasEntity>) getIntent().getExtras().getSerializable("persons");

        mList = myshareDeleteList.getRefreshView();
        mList.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        mList.setOverscrollFooter(null);
        mList.setOverscrollHeader(null);
        mList.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
        myshareDeleteList.setPullRefreshEnabled(false);
        myshareDeleteList.setPullLoadEnabled(false);//设置上拉刷新功能关闭
        adapter = new MyShareDeleteAdapter(mList, datasEntities, R.layout.item_myshare_delete);
        mList.setAdapter(adapter);
    }

    private void delete(final ArrayList<MyShareBean.DatasEntity> datasEntities_delete) {
        if (datasEntities_delete.size() > 0) {
            dialog.show();
            for (int i = 0; i < datasEntities_delete.size(); i++) {
                for (int j = 0; j < datasEntities_change.size(); j++) {
                    if (datasEntities_change.get(j).getSharerAccountId() == datasEntities_delete.get(i).getSharerAccountId())
                        datasEntities_change.remove(j);
                }
            }
            String receive_name = "";
            String receive_id = "";

            String url = AppConfig.WEB_SYSTEM + "sharePersonalSchedule.json";

            for (int i = 0; i < datasEntities_change.size(); i++) {
                receive_name += (datasEntities_change.get(i).getSharerName() + ";");
                receive_id += ("&receiverIds=" + datasEntities_change.get(i).getSharerAccountId());
            }
            url += receive_id;

            HttpParams params = new HttpParams();
            params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
            params.put("scopeFlag", "1");
            params.put("receiverNames", receive_name);


            KJHttp kjHttp = new KJHttp();
            if (datasEntities_change.size() == 0) {
                kjHttp.get(url, params, new HttpCallBack() {
                    @Override
                    public void onSuccess(String t) {
                        super.onSuccess(t);
                        ShareBean shareBean = JSON.parseObject(t, ShareBean.class);
                        if (shareBean.getStatus().equals("200")) {
                            dialog.dismiss();
                            for (int i = 0; i < datasEntities_delete.size(); i++) {
                                for (int j = 0; j < datasEntities.size(); j++) {
                                    if (datasEntities.get(j).getSharerAccountId() == datasEntities_delete.get(i).getSharerAccountId())
                                        datasEntities.remove(j);
                                }
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            dialog.dismiss();
                            datasEntities_change.clear();
                            datasEntities_change = datasEntities;
                            ViewInject.toast("删除失败");
                        }
                    }

                    @Override
                    public void onFailure(int errorNo, String strMsg) {
                        super.onFailure(errorNo, strMsg);
                        dialog.dismiss();
                        datasEntities_change.clear();
                        datasEntities_change = datasEntities;
                        ViewInject.toast(strMsg);
                    }
                });
            } else {
                kjHttp.post(url, params, new HttpCallBack() {
                    @Override
                    public void onSuccess(String t) {
                        super.onSuccess(t);
                        ShareBean shareBean = JSON.parseObject(t, ShareBean.class);
                        if (shareBean.getStatus().equals("200")) {
                            dialog.dismiss();
                            for (int i = 0; i < datasEntities_delete.size(); i++) {
                                for (int j = 0; j < datasEntities.size(); j++) {
                                    if (datasEntities.get(j).getSharerAccountId() == datasEntities_delete.get(i).getSharerAccountId())
                                        datasEntities.remove(j);
                                }
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            dialog.dismiss();
                            datasEntities_change.clear();
                            datasEntities_change = datasEntities;
                            ViewInject.toast("删除失败");
                        }
                    }

                    @Override
                    public void onFailure(int errorNo, String strMsg) {
                        super.onFailure(errorNo, strMsg);
                        dialog.dismiss();
                        datasEntities_change.clear();
                        datasEntities_change = datasEntities;
                        ViewInject.toast(strMsg);
                    }
                });
            }

        } else {
            ViewInject.toast("请选择人员");
        }
    }

    @OnClick({R.id.back, R.id.myshare_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                Bundle bundle = new Bundle();
                Intent intent = new Intent();
                bundle.putSerializable("persons", datasEntities);
                intent.putExtras(bundle);
                setResult(101, intent);
                finish();
                break;
            case R.id.myshare_delete:
                delete(adapter.getArrayList());
                break;
        }
    }
}
